package com.pounds.service.impl;

import com.pounds.mapper.MealMapper;
import com.pounds.mapper.UserMapper;
import com.pounds.pojo.Meal;
import com.pounds.service.MealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MealServiceImpl implements MealService {

    @Autowired
    private MealMapper mealMapper;

    @Autowired
    private UserMapper userMapper;

    enum MealEnumError {
        Normal,
        DataFormatError,
        DataNotFoundError
    }

    @Override
    public List<Meal> getMealList() {
        return mealMapper.getMealList();
    }

    @Override
    public String addMeal(int user, String breakfast, String lunch, String dinner, String need, String note) {
        if (user < 0) {
            return MealEnumError.DataFormatError.toString();
        }
        String name = userMapper.selectUserName(user);
        if (name == null) {
            return MealEnumError.DataNotFoundError.toString();
        } else if (name.isEmpty()) {
            return MealEnumError.DataNotFoundError.toString();
        }
        mealMapper.addMeal(user, name, breakfast, lunch, dinner, need, note);
        return MealEnumError.Normal.toString();
    }

    @Override
    public String deleteMeal(int id) {
        List<Meal> meal = mealMapper.selectMeal(id);
        if (meal == null) {
            return MealEnumError.DataNotFoundError.toString();
        }
        mealMapper.deleteMeal(id);
        return MealEnumError.Normal.toString();
    }

    @Override
    public String updateMeal(int id, int user, String breakfast, String lunch, String dinner, String need, String note) {
        if (user < 0) {
            return MealEnumError.DataFormatError.toString();
        }
        String name = userMapper.selectUserName(user);
        if (name == null) {
            return MealEnumError.DataNotFoundError.toString();
        } else if (name.isEmpty()) {
            return MealEnumError.DataNotFoundError.toString();
        }
        mealMapper.updateMeal(id, user, name, breakfast, lunch, dinner, need, note);
        return MealEnumError.Normal.toString();
    }
}
